package com.starsoft.trade.config;

import javax.annotation.PostConstruct;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;

@Component(value="tradeCacheConfig")
public class CacheConfig {

	@Autowired
	EhCacheCacheManager cacheManager;

	@PostConstruct
	public void setCache() {
		String cacheName = "traderCache";
		CacheManager cacheManagerEH = cacheManager.getCacheManager();
		if (cacheManagerEH.getCache(cacheName) != null) {
			cacheManagerEH.removeCache(cacheName);
		}

		CacheConfiguration config = new CacheConfiguration(cacheName, 100)
				.eternal(false)
				.timeToIdleSeconds(0)
				.timeToLiveSeconds(3600)
				.overflowToDisk(false)
				.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.FIFO);
		Cache cache = new Cache(config);
		cacheManagerEH.addCache(cache);
	}

}
